home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / malloc / mtrace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  4.8 KB  |  188 lines

  1. /* More debugging hooks for `malloc'.
  2.    Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.          Written April 2, 1991 by John Gilmore of Cygnus Support.
  4.          Based on mcheck.c by Mike Haertel.
  5.  
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10.  
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with this library; see the file COPYING.LIB.  If
  18. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. Cambridge, MA 02139, USA.
  20.  
  21.    The author may be reached (Email) at the address mike@ai.mit.edu,
  22.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  23.  
  24. #ifndef    _MALLOC_INTERNAL
  25. #define    _MALLOC_INTERNAL
  26. #include <malloc.h>
  27. #endif
  28.  
  29. #include <stdio.h>
  30.  
  31. #ifndef    __GNU_LIBRARY__
  32. extern char *getenv ();
  33. #else
  34. #include <stdlib.h>
  35. #endif
  36.  
  37. static FILE *mallstream;
  38. static char mallenv[]= "MALLOC_TRACE";
  39. static char mallbuf[BUFSIZ];    /* Buffer for the output.  */
  40.  
  41. /* Address to breakpoint on accesses to... */
  42. __ptr_t mallwatch;
  43.  
  44. /* File name and line number information, for callers that had
  45.    the foresight to call through a macro.  */
  46. char *_mtrace_file;
  47. int _mtrace_line;
  48.  
  49. /* Old hook values.  */
  50. static void (*tr_old_free_hook) __P ((__ptr_t ptr));
  51. static __ptr_t (*tr_old_malloc_hook) __P ((__malloc_size_t size));
  52. static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr, __malloc_size_t size));
  53.  
  54. /* This function is called when the block being alloc'd, realloc'd, or
  55.    freed has an address matching the variable "mallwatch".  In a debugger,
  56.    set "mallwatch" to the address of interest, then put a breakpoint on
  57.    tr_break.  */
  58.  
  59. void tr_break __P ((void));
  60. void
  61. tr_break ()
  62. {
  63. }
  64.  
  65. static void tr_where __P ((void));
  66. static void
  67. tr_where ()
  68. {
  69.   if (_mtrace_file)
  70.     {
  71.       fprintf (mallstream, "@ %s:%d ", _mtrace_file, _mtrace_line);
  72.       _mtrace_file = NULL;
  73.     }
  74. }
  75.  
  76. static void tr_freehook __P ((__ptr_t));
  77. static void
  78. tr_freehook (ptr)
  79.      __ptr_t ptr;
  80. {
  81.   tr_where ();
  82.   fprintf (mallstream, "- %p\n", ptr);    /* Be sure to print it first.  */
  83.   if (ptr == mallwatch)
  84.     tr_break ();
  85.   __free_hook = tr_old_free_hook;
  86.   free (ptr);
  87.   __free_hook = tr_freehook;
  88. }
  89.  
  90. static __ptr_t tr_mallochook __P ((__malloc_size_t));
  91. static __ptr_t
  92. tr_mallochook (size)
  93.      __malloc_size_t size;
  94. {
  95.   __ptr_t hdr;
  96.  
  97.   __malloc_hook = tr_old_malloc_hook;
  98.   hdr = (__ptr_t) malloc (size);
  99.   __malloc_hook = tr_mallochook;
  100.  
  101.   tr_where ();
  102.   /* We could be printing a NULL here; that's OK.  */
  103.   fprintf (mallstream, "+ %p %lx\n", hdr, (unsigned long)size);
  104.  
  105.   if (hdr == mallwatch)
  106.     tr_break ();
  107.  
  108.   return hdr;
  109. }
  110.  
  111. static __ptr_t tr_reallochook __P ((__ptr_t, __malloc_size_t));
  112. static __ptr_t
  113. tr_reallochook (ptr, size)
  114.      __ptr_t ptr;
  115.      __malloc_size_t size;
  116. {
  117.   __ptr_t hdr;
  118.  
  119.   if (ptr == mallwatch)
  120.     tr_break ();
  121.  
  122.   __free_hook = tr_old_free_hook;
  123.   __malloc_hook = tr_old_malloc_hook;
  124.   __realloc_hook = tr_old_realloc_hook;
  125.   hdr = (__ptr_t) realloc (ptr, size);
  126.   __free_hook = tr_freehook;
  127.   __malloc_hook = tr_mallochook;
  128.   __realloc_hook = tr_reallochook;
  129.   tr_where ();
  130.   if (hdr == NULL)
  131.     /* Failed realloc.  */
  132.     fprintf (mallstream, "! %p %lx\n", ptr, (unsigned long)size);
  133.   else
  134.     fprintf (mallstream, "< %p\n> %p %lx\n", ptr, hdr, (unsigned long)size);
  135.  
  136.   if (hdr == mallwatch)
  137.     tr_break ();
  138.  
  139.   return hdr;
  140. }
  141.  
  142. /* We enable tracing if either the environment variable MALLOC_TRACE
  143.    is set, or if the variable mallwatch has been patched to an address
  144.    that the debugging user wants us to stop on.  When patching mallwatch,
  145.    don't forget to set a breakpoint on tr_break!  */
  146.  
  147. void
  148. mtrace ()
  149. {
  150.   char *mallfile;
  151.  
  152.   /* Don't panic if we're called more than once.  */
  153.   if (mallstream != NULL)
  154.     return;
  155.  
  156.   mallfile = getenv (mallenv);
  157.   if (mallfile != NULL || mallwatch != NULL)
  158.     {
  159.       mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
  160.       if (mallstream != NULL)
  161.     {
  162.       /* Be sure it doesn't malloc its buffer!  */
  163.       setbuf (mallstream, mallbuf);
  164.       fprintf (mallstream, "= Start\n");
  165.       tr_old_free_hook = __free_hook;
  166.       __free_hook = tr_freehook;
  167.       tr_old_malloc_hook = __malloc_hook;
  168.       __malloc_hook = tr_mallochook;
  169.       tr_old_realloc_hook = __realloc_hook;
  170.       __realloc_hook = tr_reallochook;
  171.     }
  172.     }
  173. }
  174.  
  175. void
  176. muntrace ()
  177. {
  178.   if (mallstream == NULL)
  179.     return;
  180.  
  181.   fprintf (mallstream, "= End\n");
  182.   fclose (mallstream);
  183.   mallstream = NULL;
  184.   __free_hook = tr_old_free_hook;
  185.   __malloc_hook = tr_old_malloc_hook;
  186.   __realloc_hook = tr_old_realloc_hook;
  187. }
  188.